import { topCategory } from '@/api/constants'
import { findAllCategory } from '@/api/category'
export default {
  namespaced: true,
  state () {
    return {
      // 分类信息 初始化时要有默认数据以免闪屏
      list: topCategory.map(item => ({ name: item }))// 数组转换为对象
    }
  },
  mutations: {
    setList (state, payload) {
      state.list = payload
    },
    // 显示菜单栏
    show (state, id) {
      const currCategory = state.list.find(item => item.id === id)
      currCategory.open = true
    },
    // 隐藏菜单栏
    hide (state, id) {
      const currCategory = state.list.find(item => item.id === id)
      currCategory.open = false
    }
  },
  actions: {
    // 渲染数据
    async getList (context) {
      const data = await findAllCategory()
      // 给每一项加上显示控制属性
      data.result.forEach(top => {
        top.open = false
      })
      context.commit('setList', data.result)
    }
  }
}
